/*
 * SelfIteratingPrintingList.java
 *
 * Created on 4 January 2008, 16:20
 *
 * Copyright (C)
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
package com.grubby.databean;

import com.grubby.databean.exception.DataOutputException;
import java.beans.IntrospectionException;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author A. HARMEL-LAW. E: andrew.harmel.law@gmail.com
 */
public class SelfIteratingPrintingList implements SelfIteratingPrintingListIntf {

    private List<AbstractDataBean> wrappedList;
    private Writer contentWriter;

    // CONSTRUCTORS //
    public SelfIteratingPrintingList() {
        setWrappedList(new ArrayList());
    }

    public SelfIteratingPrintingList(Writer aWriter) {
        super();
        contentWriter = aWriter;
    }

    private SelfIteratingPrintingList(List aList) {
        setWrappedList(aList);
    }

    /**
     * 
     * @param interBeanOutput
     * @throws com.grubby.databean.exception.DataOutputException
     */
    @Override
    public void outputAsSql(String interBeanOutput) throws DataOutputException {

        // Check contentWriter is not null
        if (contentWriter == null) {
            throw new DataOutputException("The content Writer was not set.  You must set it before you use this.");
        }

        // Iterate through the wrappedList and print each bean as required
        for (AbstractDataBean currentBean : getWrappedList()) {
            try {
                contentWriter.write(currentBean.toSql() + interBeanOutput);
            // TODO: Need to think about storing the fields to write on the beans themselves
            // TODO: Cascade down to any wrapped list values which the bean may contain
            } catch (IOException ex) {
                Logger.getLogger(SelfIteratingPrintingList.class.getName()).log(Level.SEVERE, null, ex);
                throw new DataOutputException("An IOException was thrown while writing a databean:", ex);
            } catch (NoSuchFieldException ex) {
                Logger.getLogger(SelfIteratingPrintingList.class.getName()).log(Level.SEVERE, null, ex);
                throw new DataOutputException("A NoSuchFieldException was thrown while writing a databean:", ex);
            } catch (IntrospectionException ex) {
                Logger.getLogger(SelfIteratingPrintingList.class.getName()).log(Level.SEVERE, null, ex);
                throw new DataOutputException("An IntrospectionException was thrown while writing a databean:", ex);
            }
        }

    }

    /**
     * 
     * @param interBeanOutput
     * @throws com.grubby.databean.exception.DataOutputException
     */
    @Override
    public void outputAsCsv(String interBeanOutput) throws DataOutputException {

        // Check contentWriter is not null
        if (contentWriter == null) {
            throw new DataOutputException("The content Writer was not set.  You must set it before you use this.");
        }

        // Iterate through the wrappedList and print each bean as required
        for (AbstractDataBean currentBean : getWrappedList()) {
            try {
                contentWriter.write(currentBean.toCsv() + interBeanOutput);
            // TODO: Need to think about storing the fields to write on the beans themselves
            // TODO: Cascade down to any wrapped list values which the bean may contain
            } catch (IOException ex) {
                Logger.getLogger(SelfIteratingPrintingList.class.getName()).log(Level.SEVERE, null, ex);
                throw new DataOutputException("An IOException was thrown while writing a databean:", ex);
            } catch (IntrospectionException ex) {
                Logger.getLogger(SelfIteratingPrintingList.class.getName()).log(Level.SEVERE, null, ex);
                throw new DataOutputException("An IntrospectionException was thrown while writing a databean:", ex);
            }
        }

    }

    /**
     * 
     * @param interBeanOutput
     * @throws com.grubby.databean.exception.DataOutputException
     */
    @Override
    public void outputAsLdif(String interBeanOutput) throws DataOutputException {

        // Check contentWriter is not null
        if (contentWriter == null) {
            throw new DataOutputException("The content Writer was not set.  You must set it before you use this.");
        }

        // Iterate through the wrappedList and print each bean as required
        for (AbstractDataBean currentBean : getWrappedList()) {
            try {
                contentWriter.write(currentBean.toCsv() + interBeanOutput);
                // TODO: Need to think about storing the fields to write on the beans themselves
                // TODO: Cascade down to any wrapped list values which the bean may contain
            } catch (IOException ex) {
                Logger.getLogger(SelfIteratingPrintingList.class.getName()).log(Level.SEVERE, null, ex);
                throw new DataOutputException("An IOException was thrown while writing a databean:", ex);
            } catch (IntrospectionException ex) {
                Logger.getLogger(SelfIteratingPrintingList.class.getName()).log(Level.SEVERE, null, ex);
                throw new DataOutputException("An IntrospectionException was thrown while writing a databean:", ex);
            }
        }

    }

    public List<AbstractDataBean> getWrappedList() {
        return wrappedList;
    }

    public void setWrappedList(List<AbstractDataBean> wrappedList) {
        this.wrappedList = wrappedList;
    }

    /**
     * Wraps the java.util.List operation to return a new SelfIteratingPrintingList
     * @param fromIndex
     * @param toIndex
     * @return
     */
    @Override
    public SelfIteratingPrintingListIntf subList(int fromIndex, int toIndex) {
        return new SelfIteratingPrintingList(wrappedList.subList(fromIndex, toIndex));
    }
}
